# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openimagedenoise
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.0
pkgrel=1
pkgdesc="Intel Open Image Denoise library of high-performance, high-quality denoising filters for images rendered with ray tracing (mingw-w64)"
arch=('any')
url="https://openimagedenoise.github.io/"
license=("Apache 2.0")
depends=("${MINGW_PACKAGE_PREFIX}-intel-tbb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
options=('strip' 'buildflags' '!debug')
source=(#${_realname}-${pkgver}.tar.gz::https://github.com/OpenImageDenoise/oidn/archive/v${pkgver}.tar.gz
        ${_realname}-${pkgver}.tar.gz::https://github.com/OpenImageDenoise/oidn/releases/download/v${pkgver}/oidn-${pkgver}.src.tar.gz
        001-build-fixes.patch)
sha256sums=('4dd484abea8a0b3d12d346343fcb1ab7abef8f94318d8c537f69a20c2a75c4eb'
            'e4f9046d6f1a189868a67e5cd25ed6e2906c4e39914e97e1ce8ff72066b54fa9')

prepare() {
  cd ${srcdir}/oidn-${pkgver}
  patch -p1 -i ${srcdir}/001-build-fixes.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    ../oidn-${pkgver}

  make
}

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install
  install -Dm644 ${srcdir}/oidn-${pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
